package com.ruoyi.framework.websocket;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.constant.ReceivedMessage;

import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

public class MessageEncoder implements Encoder.Text<ReceivedMessage> {
    private static ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String encode(ReceivedMessage message) throws EncodeException {
        try {
            return objectMapper.writeValueAsString(message);
        } catch (Exception e) {
            throw new EncodeException(message, "对象编码失败", e);
        }
    }

    @Override
    public void init(EndpointConfig config) {
        // 初始化编码器
    }

    @Override
    public void destroy() {
        // 销毁编码器
    }
}